package datagramSocket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * UDP 服务器端响应 并返回 ok
 * @author Administrator
 *
 */
public class DatagramSocketServer {
	public static void main(String[] agrs){
		//建立连接
		DatagramSocket ds = null;
//		DatagramPacket sendDp ;      //发送
		DatagramPacket receiveDp;    //接受
		final int PORT = 10086;
		
		//初始化接受数据
		byte[] b = new byte[1024];
		receiveDp = new DatagramPacket(b, b.length);
		
		try{
			//建立连接 监听端口
			ds = new DatagramSocket(PORT);
			System.out.println("DatagramSocket 服务器已启动");
			int i = 0;
			while(true){
				//接受
				ds.receive(receiveDp);
				
				new LogicThread( ds, receiveDp);
				
				System.out.println("第" + ++i + " 次访问");
			}
			/*//输出
			InetAddress clientIP = receiveDp.getAddress();
			int clientPort = receiveDp.getPort();
			byte[] data = receiveDp.getData();
			int len = receiveDp.getLength();
			
			System.out.println(" 客户端ip=" + clientIP.getHostAddress() + "; \n 端口=" + clientPort);
			System.out.println(" 内容：" + new String( data, 0, len));
			
			//反馈
			String response = "OK";
			byte[] bData = response.getBytes();
			sendDp = new DatagramPacket(bData, bData.length, clientIP, clientPort);
			
			ds.send(sendDp);
			*/
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			ds.close();
		}
	}
}
